﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNetMVC.DTO;
using DotNetMVC.IService;
using DotNetMVC.Model;

namespace DotNetMVC.Service
{
    public partial class RoleService : BaseService<Role>, IRoleService
    {
        private IQueryable<Role> GetJoinAll()
        {
            return GetAll()
                .Include(r => r.RolePermissions)
                .Include(r => r.RolePermissions.Select(rp => rp.Permission))
                .AsNoTracking();
        }
        /// <summary>
        /// 角色拥有权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public RoleDTO[] GetAdminRoles(long adminId)
        {
            //管理员是否存在
            AdminUser user = DbSession.AdminUserDal.GetAll()
                .Include(u => u.AdminUserRoles)
                 .Include(u => u.AdminUserRoles.Select(ar => ar.Role))
                .Include(u => u.AdminUserRoles.Select(ar => ar.Role.RolePermissions))
                .Include(u => u.AdminUserRoles.Select(ar => ar.Role.RolePermissions.Select(rp => rp.Permission)))
                .AsNoTracking().SingleOrDefault(u=>u.Id== adminId);
            if (user == null)
            {
                throw new ArgumentException("获取不到管理员(Id:" + adminId + ")信息");
            }
            return user.AdminUserRoles.Select(ar => ToDTO(ar.Role)).ToArray();
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public RoleDTO[] GetRoles()
        {
            return GetJoinAll().ToList().Select(r=> ToDTO(r)).ToArray();
        }

        public RoleDTO GetDTOById(long id)
        {
            var roles = GetJoinAll().Where(r=> r.Id == id);
            int count = roles.Count();
            if (count <= 0)
            {
                return null;
            }
            else if (count == 1)
            {
                return ToDTO(roles.Single());
            }
            else
            {
                throw new ApplicationException("获取到多个 Id 为" + id + "的角色");
            }
        }

        private RoleDTO ToDTO(Role entity)
        {
            RoleDTO dto = new RoleDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.Name = entity.Name;
            dto.Description = entity.Description;
            dto.PermissionIds = entity.RolePermissions.Select(rp=>rp.Permission.Id).ToList();
            dto.PermissionNames = string.Join(",", entity.RolePermissions.Select(rp => rp.Permission.Name));
            return dto;
        }

        public long Add(string name, string description)
        {
            Role role = new Role();
            role.Name = name;
            role.Description = description;
            //新增角色
            return Add(role).Id;
        }
        public void Update(long id, string name, string description)
        {
            Role role = GetById(id);
            if (role==null)
            {
                throw new Exception("获取不到角色(id:"+id+")信息");
            }
            role.Name = name;
            role.Description = description;
            Update(role);
        }
    }
}
